package
{
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.TileList;
	import mx.events.DragEvent;
	import mx.managers.DragManager;

	public class fmborder extends Canvas
	{
		/**
		 * 原始数据
		 */
		private var _ori_width:Number; //宽
		private var _ori_height:Number; //高
		private var _ori_img_original:String; //原始图片的地址
		private var _ori_thumb_url:String; //缩累
		private var _ori_img_desc:String; //说明
		private var _ori_img_id:String; //id
		private var _ori_goods_id:String; //商品编号
		private var _ori_hotarea:String; //热区数据xml格式
		private var _ori_array:String;
		
		private var img:Image;
		
		private var flag_autoid:int = 0; //
		
		public function fmborder(args:String)
		{
			super();
			//保存原始数据
			this._ori_array = args;
			
			addBorder();
			addHotarea();
			
		}
		
		private function addBorder():void{
			var tmp:XML = XML(this._ori_array);
			
			//添加border img
			img = new Image();
			img.percentWidth = 100;
			img.percentHeight = 100;
			img.source = tmp.urlx;
			addChild(img);
			this.width = tmp.width;
			this.height = tmp.height;
		}
		
		private function addHotarea():void{
			var tmp:XML = XML(this._ori_array);
			
			//添加hotarea
			if(tmp.hotarea!=''){
				var t3:XML = XML(tmp.hot);
				//Alert.show(t3);
				for each(var ss:XML in t3.hoterea.info){
					var ncs:Number = this.flag_autoid++;
					var ha1:hoterea = new hoterea();
					ha1.name = 'hotarea'+ncs;
					ha1.id = ha1.name;
					ha1.width = ss.width;
					ha1.height = ss.height;
					ha1.x = ss.x;
					ha1.y = ss.y;
					//ob.registerComponent(ha1, ha1);
					ha1.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
    				ha1.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
    				ha1.doubleClickEnabled = true;
    				//ha1.addEventListener(MouseEvent.DOUBLE_CLICK, function (e:MouseEvent){hotareadbclick(e, ha1);});
					addChild(ha1);
					//Alert.show(ha1.width.toString());
				}
				
			}
			
		}
		
		// mainid的dragEnter事件
			private function dragEnterHandler(event:DragEvent):void {
    			DragManager.acceptDragDrop(Canvas(event.currentTarget));
			}
			//鼠标释放
			private  function dragDropHandler(event:DragEvent):void {
    			var hotname:String = '';
    			var hta1:hoterea; 
    			if(event.currentTarget is hoterea){
    				hotname = event.currentTarget.name;
    				//Alert.show(hotname);
    				hta1 = this.getChildByName(hotname) as hoterea;
    			}
    			
    			var tl:TileList = event.dragInitiator as TileList;
    			var imgarray:Object = {name: tl.selectedItem.name,
    									urlx:tl.selectedItem.urlx, 
    									width:tl.selectedItem.width,
    									height:tl.selectedItem.height,
    									thumb:tl.selectedItem.thumb,
    									idx:tl.selectedItem.idx };
    			var image:Image = new Image;
    			var htn1:String = '';
    			if(event.currentTarget is hoterea){
    				Alert.show(event.currentTarget.name);
    				htn1 = event.currentTarget.name;
    				//Alert.show(this.getChildByName(htm1).id);
    			}
				image.source = ((event.dragInitiator as TileList).selectedItem.urlx);
				//var can1:hoterea = event.currentTarget as hoterea;
				//trace(event.currentTarget.toString());
				trace(hta1.width+'|'+hta1.height);
				image.percentWidth = 100;
				image.percentHeight = 100;
				image.maintainAspectRatio = false;
				hta1.addChild(image);
				hta1.setImgData(imgarray);
				trace(hta1.name);
			}
	}
}